package com.jms.service.cms.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jms.dao.BaseDaoI;
import com.jms.model.cms.Tcate;
import com.jms.model.cms.Tnews;
import com.jms.model.sys.Tuser;
import com.jms.pageModel.base.PageFilter;
import com.jms.pageModel.cms.News;
import com.jms.service.cms.NewsServiceI;
import com.jms.utils.StringUtil;

@Service
public class NewsServiceImpl implements NewsServiceI {

	@Autowired
	private BaseDaoI<Tnews> newsDao;
	@Autowired
	private BaseDaoI<Tcate> cateDao;
	@Autowired
	private BaseDaoI<Tuser> userDao;
	

	@Override
	public List<News> dataGrid(News news, PageFilter ph) {
		List<News> newslist = new ArrayList<News>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "from Tnews t ";
		List<Tnews> l = newsDao.find(hql+whereHql(news, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tnews t : l) {
			News n = new News();
			BeanUtils.copyProperties(t, n);
			if (t.getCategory() != null) {
				n.setCategoryId(t.getCategory().getId());
				
				n.setCategoryName(t.getCategory().getName());
			}
			if(t.getPublishedby()!=null){
				n.setPublishedbyid(t.getPublishedby().getId());
				n.setPublishedByName(t.getPublishedby().getName());
			}
			newslist.add(n);
		}
		
		return newslist;

	}
	public String whereHql(News news,Map<String, Object> params){
		String hql = "";
		if (news != null) {
			hql += " where 1=1 ";
			if (news.getTitle() != null&&!"".endsWith(news.getTitle())) {
				hql += " and t.title like :title";
				params.put("title", "%%" + news.getTitle() + "%%");
			}
			if (news.getPublishedByName() != null&&!"".endsWith(news.getPublishedByName())) {
				Tuser u = userDao.get("from Tuser t where t.name like '%"+news.getPublishedByName()+"%'");
				hql += " and t.publishedby = :publishedby";
				params.put("publishedby", u);
			}
			if (news.getCreatedatetimeStart() != null) {
				hql += " and t.pubtime >= :createdatetimeStart";
				params.put("createdatetimeStart", news.getCreatedatetimeStart());
			}
			if (news.getCreatedatetimeEnd() != null) {
				hql += " and t.pubtime <= :createdatetimeEnd";
				params.put("createdatetimeEnd", news.getCreatedatetimeEnd());
			}
			if(news.getStatus()!=null){
				hql += " and t.status = :status";
				params.put("status", news.getStatus());
			}
			if(news.getCategoryId()!=null){
				Tcate tc = cateDao.get(Tcate.class, news.getCategoryId());
				hql += " and t.category.level like :level";
				params.put("level",tc.getLevel()+"%");
			}
		}
		return hql;
	}
	public String orderHql(PageFilter ph){
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	@Override
	public Long count(News news, PageFilter ph) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tnews t ";
		return newsDao.count("select count(*) " + hql + whereHql(news, params), params);
		
	}

	@Override
	public void add(News news) {
		Tnews t = new Tnews();
		StringUtil.beanCopy(news, t, true);
		if(news.getCategoryId()!=null){
			t.setCategory(cateDao.get(Tcate.class, news.getCategoryId()));
		}
		if(news.getPublishedbyid()!=null){
			t.setPublishedby(userDao.get(Tuser.class, news.getPublishedbyid()));
		}
		
		newsDao.save(t);
		
	}

	@Override
	public void recycle(Long id) {
		Tnews t = newsDao.get(Tnews.class, id);
		t.setStatus(2);//放入回收站
		newsDao.update(t);
		
	}
	@Override
	public void delete(Long id) {
		Tnews t = newsDao.get(Tnews.class, id);
		del(t);
		
	}
	private void del(Tnews t) {
		newsDao.delete(t);
	}
	@Override
	public void edit(News news) {
		Tnews t = newsDao.get(Tnews.class, news.getId());
		StringUtil.beanCopy(news, t, true);
		if(news.getCategoryId()!=null){
			t.setCategory(cateDao.get(Tcate.class, news.getCategoryId()));
		}
		if(news.getPublishedbyid()!=null){
			t.setPublishedby(userDao.get(Tuser.class, news.getPublishedbyid()));
		}
		if(news.getModifiedbyid()!=null){
			t.setModifiedby(userDao.get(Tuser.class, news.getModifiedbyid()));
		}
		newsDao.update(t);
		
	}
	@Override
	public News get(Long id) {
		Tnews tnews = newsDao.get(Tnews.class, id);
		News news = new News();
		StringUtil.beanCopy(tnews, news, true);
		if(tnews.getCategory()!=null){
			news.setCategoryId(tnews.getCategory().getId());
			news.setCategoryName(tnews.getCategory().getName());
		}
		if(tnews.getPublishedby()!=null){
			news.setPublishedbyid(tnews.getPublishedby().getId());
			news.setPublishedByName(tnews.getPublishedby().getName());
		}
		return news;
	}
	@Override
	public void move(Long id, Long cid) {
		Tnews t = newsDao.get(Tnews.class, id);
		Tcate c = cateDao.get(Tcate.class, cid);
		t.setCategory(c);
		newsDao.update(t);
	}


	
}
